package cn.hiapi.admin.api;

import cn.hiapi.admin.query.ActivityGroupOrderQuery;
import cn.hiapi.admin.vo.ActivityGroupOrderVo;
import cn.hiapi.admin.vo.ActivityOrderVo;
import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.user.IUserService;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shopkeeper.entity.activity.ActivityGroupBuy;
import cn.hiapi.shopkeeper.entity.order.ActivityGroupOrder;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.order.ActivityGroupOrderService;
import cn.hiapi.shopkeeper.service.order.ActivityOrderService;
import cn.hiapi.utils.JsonUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/2/5 3:41
 */
@RestController
@RequestMapping("/manage/group-order")
public class AdminGroupController extends BasicQueryController<ActivityGroupOrder, Long, ActivityGroupOrderVo, ActivityGroupOrderQuery> {

    protected AdminGroupController(ActivityGroupOrderService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }


    @Override
    public ActivityGroupOrderQuery setQuery(ActivityGroupOrderQuery query) {
        if (StringUtils.isNotEmpty(query.getShopName())) {
            List<Shop> list = this.dispatchContext.getServiceOne(ShopService.class).findByName(query.getShopName());
            if (list.isEmpty()) throw new BasicException("店铺名字不存在");
            query.setShopIds(list.stream().map(Shop::getId).collect(Collectors.toList()));
        }
        if (StringUtils.isNotEmpty(query.getMobile())) {
            Long uid = this.dispatchContext.getServiceOne(IUserService.class).getUid(0, query.getMobile());
            if (uid == null) throw new BasicException("手机号不存在");
            query.setUid(uid);
        }
        return super.setQuery(query);
    }

    @Override
    public List<ActivityGroupOrderVo> toListVo(List<ActivityGroupOrder> list, String[] fields) {
        return this.toList(list, fields);
    }

    @Override
    public ActivityGroupOrderVo toData(ActivityGroupOrder data, String[] fields) {
        ActivityGroupOrderVo vo = data.copy(new ActivityGroupOrderVo());

        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        if (shop != null) {
            vo.setShop(JsonUtils.builder()
                    .put("id", shop.getId())
                    .put("name", shop.getName())
                    .put("logo", shop.getLogo())
                    .build());
        }

        Object user = this.dispatchContext.getServiceOne(IUserService.class).fieldBase(data.getBuyerId());
        vo.setUser(user);

        return vo;
    }


    @ApiOperation(value = "关联团购的订单", notes = "关联团购的订单", nickname = "AdinZ")
    @GetMapping("/orders")
    public ResponseEntity orders(@RequestParam("id") long id) {
        List<ActivityOrder> list = this.dispatchContext.getServiceOne(ActivityOrderService.class)
                .findList(new QueryWrapper().eq("fid", id).eq("activityType", ActivityType.GROUP));
        List<ActivityOrderVo> collect = list.stream().map(e -> {
            ActivityOrderVo vo = e.copy(new ActivityOrderVo());
            Object base = this.dispatchContext.getServiceOne(IUserService.class).fieldBase(e.getBuyerId());
            vo.setBuyer(base);
            return vo;
        }).collect(Collectors.toList());
        return toSuccess(collect);
    }


}
